<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Controller;
use App\Models\Paper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use zgldh\QiniuStorage\QiniuStorage;

class PaperController extends Controller
{

  //列表
  public function index(Request $request)
  {
    $papers = Paper::getList($request);
    return $this->success($papers);
  }

  // 详情
  public function detail($id)
  {
    $paperInfo = Paper::getInfo($id);
    return $this->success($paperInfo);
  }

  // 新增
  public function add(Request $request)
  {
    $data = $request->all();
    $rule = [
      'title'     => 'required',
      'cateid'    => 'required',
      'img'       => 'required',
    ];
    $message = [
      'title.required'       => '标题不能为空',
      'cateid.required'      => '分类不能为空',
      'img.required'         => '图片不能为空'
    ];
    $validated = $this->validator($data, $rule, $message);
    if (is_bool($validated)) {
      $insertData = [
        'uid' => '1',
        'uname' => '潇洒',
        'uavatar' => 'https://lhsk.demo.hongcd.com/uploads/20230203/thumb_17758e946f5d1ad24457865070cdb4f4.jpg',
        'title' => $request->title,
        'img' => $request->img,
        'description' => $request->description,
        'lx' => 4,
        'lxname' => '图片',
        'cateid' => $request->cateid,
        'catename' => $request->catename,
        'status' => $request->status,
        'score' => $request->score,
        'is_hot' => $request->is_hot,
        'download_num' => $request->download_num,
        'collect_num' => $request->collect_num,
        'love_num' => $request->love_num,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('papers')->insert($insertData);
      return $this->success();
    } else {
      return $validated;
    }
  }

  // 修改
  public function update(Request $request)
  {
    $paperInfo = DB::table('papers')->where('id', $request->id)->first();
    if ($paperInfo && $paperInfo->id) {
      $updateData = [
        'title' => $request->title ?? $paperInfo->title,
        'img' => $request->img ?? $paperInfo->img,
        'description' => $request->description ?? $paperInfo->description,
        'lx' => $request->lx ?? $paperInfo->lx,
        'lxname' => $request->lxname ?? $paperInfo->lxname,
        'cateid' => $request->cateid ?? $paperInfo->cateid,
        'catename' => $request->catename ?? $paperInfo->catename,
        'status' => $request->status ?? $paperInfo->status,
        'score' => $request->score ?? $paperInfo->score,
        'is_hot' => $request->is_hot ?? $paperInfo->is_hot,
        'download_num' => $request->download_num ?? $paperInfo->download_num,
        'collect_num' => $request->collect_num ?? $paperInfo->collect_num,
        'love_num' => $request->love_num ?? $paperInfo->love_num,
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('papers')->where('id', $request->id)->update($updateData);
      if ($result) {
        return $this->success();
      }
      return $this->failed('修改失败');
    } else {
      return $this->failed('不存在');
    }
  }

  // 删除
  public function delete($id)
  {
    $paperInfo = DB::table('papers')->where('id', $id)->first();
    if ($paperInfo && $paperInfo->img) {
      $idx = strrpos($paperInfo->img, '/');
      $fileName = $idx ? substr($paperInfo->img, $idx + 1) : '';
      $key = 'wallpaper/' . $fileName;
      // 删除七牛图片资源
      $disk = QiniuStorage::disk('qiniu');
      if ($disk->exists($key)) {
        $disk->delete($key);
      }
      $result = DB::table('papers')->where('id', $id)->delete();
      if ($result) {
        return $this->success();
      }
    }
    return $this->failed('删除失败');
  }
}
